﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace AnimationTester
{
    public class SpriteAnimation : SpriteManager
    {
        private float timeElapsed;
        public bool isLooping = false;

        // default to 20 frames per second
        private float timeToUpdate = 0.05f;
        public int framesPerSecond
        {
            set { timeToUpdate = (1f / value); }
        }

        public SpriteAnimation(Texture2D texture, int frames)
            : base(texture, frames)
        {
        }

        public void Update(GameTime gameTime)
        {
            timeElapsed += (float)
                gameTime.ElapsedGameTime.TotalSeconds;

            if (timeElapsed > timeToUpdate)
            {
                timeElapsed -= timeToUpdate;

                if (frameIndex < rectangles.Length - 1)
                    frameIndex++;
                else if (isLooping)
                    frameIndex = 0;
            }
        }
    }
}
